package com.bw.zonghe0719.Login

import androidx.lifecycle.viewModelScope
import com.bw.base.entity.CodeSendEntity
import com.bw.base.entity.LoginEntity
import com.bw.base.entity.Res
import com.bw.base.entity.UserInfoEntity
import com.bw.base.mvvm.BaseViewModel
import com.bw.base.utils.SingleLiveData
import kotlinx.coroutines.launch

class LoginViewModel:BaseViewModel() {
    //获取验证码
    val codeSendSuccess=SingleLiveData<Res<CodeSendEntity>>()
    val codeSendFail=SingleLiveData<String>()
    fun codeSend(phone:String){
        viewModelScope.launch {
            try {
                codeSendSuccess.postValue(apiservice.codeSend(phone))
            }catch (e:Exception){
                codeSendFail.postValue(e.message.toString())
            }
        }
    }
    //验证码登录
    val loginCodeSuccess=SingleLiveData<Res<LoginEntity>>()
    val loginCodeFail=SingleLiveData<String>()
    fun loginCode(captcha:String,phone:String){
        viewModelScope.launch {
            try {
                var map= mutableMapOf<String,Any>()
                map.put("captcha",captcha)
                map.put("phone",phone)
                loginCodeSuccess.postValue(apiservice.loginCode(mapToBody(map)))
            }catch (e:Exception){
                loginCodeFail.postValue(e.message.toString())
            }
        }
    }
    //用户信息
    val userInfoSuccess=SingleLiveData<Res<UserInfoEntity>>()
    val userInfoFail=SingleLiveData<String>()
    fun userInfo(){
        viewModelScope.launch {
            try {
                userInfoSuccess.postValue(apiservice.userInfo())
            }catch (e:Exception){
                userInfoFail.postValue(e.message.toString())
            }
        }
    }
    //手机号修改密码
    val updatePwdSuccess=SingleLiveData<Res<Boolean>>()
    val updatePwdFail=SingleLiveData<String>()
     fun updatePwd(account:String,captcha:String,password:String){
         viewModelScope.launch {
             try {
                 var map=HashMap<String,Any>()
                 map.put("account",account)
                 map.put("captcha",captcha)
                 map.put("password",password)
                 updatePwdSuccess.postValue(apiservice.updatePwd(mapToBody(map)))
             }catch (e:Exception){
                 updatePwdFail.postValue(e.message.toString())
             }
         }
     }
    //账号密码登录
    val loginPwdSuccess=SingleLiveData<Res<LoginEntity>>()
    val loginPwdFail=SingleLiveData<String>()
    fun loginPwd(account:String,password:String){
        viewModelScope.launch {
            try {
                var map=HashMap<String,Any>()
                map.put("account",account)
                map.put("password",password)
                loginPwdSuccess.postValue(apiservice.login(mapToBody(map)))
            }catch (e:Exception){
                loginPwdFail.postValue(e.message.toString())
            }
        }
    }

}